﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Evolution
{
    class Program
    {
        /// <summary>
        /// Вывод обозначения на экран
        /// </summary>
        static void Legend()
        {
            Console.WriteLine("LEGEND\n");
            Console.WriteLine(" -' '(space)  -  free cel");
            Console.Write(" -\'");
            (new Vegetarian(0,0)).Show();
            Console.WriteLine("\'         -  vegetarian cel");
            Console.Write(" -\'");
            (new Predator(0, 0)).Show();
            Console.Write("\'         -  predator cel\n");
            Console.WriteLine("SPACE         -  evolution");
            Console.WriteLine("Ctrl+A        -  save field to file");
            Console.WriteLine("ESC           -  Exit");
            Console.WriteLine("Make your choice...");
        }

        /// <summary>
        /// функция сохранения матрицы 
        /// </summary>
        /// <param name="e"></param>
        static void save(Evol e)
        {
            Console.Clear();
            Console.WriteLine("SAVING:\n");
            Console.Write("Write name of file: ");
            string fname = Console.ReadLine();
            StreamWriter wr;
            if ((wr = new StreamWriter(fname)) != null)
            {
                e.SaveFieldToFile(wr);
                wr.Close();
            }
        }

        /// <summary>
        /// фкнция меин
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Console.WindowHeight = 50;
            Console.WindowWidth = 60;
            Console.Title = "Evolution";

            ConsoleKeyInfo key;
            Evol evol = new Evol(MyConst.COUNT);
            while (true)
            {
                Console.Clear();
                Console.WriteLine("Evolution 2.0 by Andrew Solomonov\n");
                Console.WriteLine("Press Ctrl+N to create new field({0}x{0}) or",MyConst.COUNT);
                Console.WriteLine("Press Ctrl+O to open a file");
                Console.WriteLine("Make your choice...");
                key = Console.ReadKey();                
                switch ((int)key.KeyChar)
                {
                    case 15://Opening Ctrl+O
                        Console.Clear();
                        Console.WriteLine("Opening file:");
                        Console.Write("File name: ");
                        string fname = Console.ReadLine();
                        StreamReader reader;
                        if (File.Exists(fname))
                        {
                            reader = new StreamReader(fname);
                            evol.LoadFieldFromFile(reader);
                            reader.Close();
                        }
                        else
                        {
                            evol.GenerateNewField();
                            Console.WriteLine("File not found!!!");
                            Console.WriteLine("Field is generated automaticaly ({0}x{0})",MyConst.COUNT);
                            Console.WriteLine("Press enter to continue");
                            Console.ReadLine();
                        }
                        break;
                    case 14:
                        evol.GenerateNewField();
                        break;
                    default:
                        continue;                                                
                }
                break;
            }

            int tmp;
            while (true)
            {
                Console.Clear();
                evol.ShowField();
                Legend();
                key = Console.ReadKey();
                tmp = (int)(key.KeyChar);
                switch (tmp)
                {
                    case 1:
                        save(evol);
                        continue;
                    case 32:
                        evol.Update();
                        continue;                    
                    case 27:
                        break;
                    default:
                        continue;
                }
                break;
            }

            Console.WriteLine("\n\nPress enter to continue...");
            Console.ReadLine();
        }
    }
}
